package Arithmetic.Array;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-10-14
 * Time: 22:54
 */
//滑动窗口
public class SlidingWindow {

    //209.长度最小的子数组
    /**
     * 时间复杂度：
     * 空间复杂度：
     * @param target 满足窗口条件的最小值
     * @param nums 目标数组
     * @return 子数组最小长度
     */
    public int minSubArrayLen(int target, int[] nums) {
        //将最小长度置为int最大值，保证
        int minLen = Integer.MAX_VALUE;

        //定义left,right双指针，动态维护窗口
        //right指针用来拓宽窗口右边界`
        //left指针用来缩小窗口左边界
        int left = 0;
        int right = 0;
        int sum = 0;
        for (; right < nums.length; right++) {
            sum += nums[right];
            while(sum >= target){
                //更新子数组最小值,并缩小左边界,直到不满足条件
                minLen = Math.min(minLen,right - left + 1);
                sum -= nums[left++];
            }
        }
        //防止没有一个子数组满足条件
        if(left == nums.length) return 0;
        return minLen;
    }

    public static void main(String[] args) {
        int[] array1 = {2,3,1,2,4,3};
        SlidingWindow slidingWindow = new SlidingWindow();
        int ret = slidingWindow.minSubArrayLen(7,array1);
        System.out.println(ret);

    }
}
